import store from '@/store'
import apiScope from '@/api/apiScope';
import idResource from '@/api/idResource';

export function useApiScopeStore() {
  return apiScopeStore(store);
}

interface StateModel {
  apiScopes: { label: string, value: string }[]
  idResource: { label: string, value: string }[]
}

const apiScopeStore = defineStore({
  id: 'apiscope',
  state: (): StateModel => ({
    apiScopes: [],
    idResource: []
  }),
  getters: {
    scopes(): { label: string, options: { label: string, value: string }[] }[] {
      return [{
        label: '身份资源',
        options: this.idResource
      },
      {
        label: '权限',
        options: this.apiScopes
      }]
    }
  },
  actions: {
    async all() {
      var apiScopeRes = await apiScope.list()
      this.apiScopes = apiScopeRes.result

      var idResourceRes = await idResource.list()
      this.idResource = idResourceRes.result
    }
  }
})

export default apiScopeStore
